﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////


#include "ExprAssistantDlg.h"
#include "ui_ExprAssistantDlg.h"
#include "TransferOwnership.h"
#include <QToolTip>
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"
#include "TranslatorManager.h"

#pragma execution_character_set("utf-8")

using namespace gcmp;

namespace gfam
{
    ExprAssistantDlg::ExprAssistantDlg(QWidget *parent)
        : QDialog(parent, Qt::Dialog | Qt::FramelessWindowHint)
    {
        ui = NEW_AS_OWNER_PTR(Ui::ExprAssistantDlg);
        DBG_WARN_AND_RETURN_VOID_UNLESS(ui, L"ui无效", L"paramodel", L"2024-02-20");
        ui->setupUi(this);
        this->setStyleSheet("QDialog {border: 1px solid #CCCCCC;}");

        DBG_WARN_AND_RETURN_VOID_UNLESS(ui->gbmp_title, L"ui->gbmp_title无效", L"paramodel", L"2024-02-20");
        ui->gbmp_title->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
        ui->gbmp_title->setIconVisible(false);
        ui->gbmp_title->setMaxButtonVisible(false);
        ui->gbmp_title->setMinButtonVisible(false);
        ui->gbmp_title->setText(QString::fromStdWString(GBMP_TR(L"表达式函数")));

        DBG_WARN_AND_RETURN_VOID_UNLESS(ui->tableWidget, L"ui->tableWidget无效", L"paramodel", L"2024-02-20");
        ui->tableWidget->setRowCount(14);
        ui->tableWidget->setColumnCount(3);
        DBG_WARN_AND_RETURN_VOID_UNLESS(ui->tableWidget->verticalHeader(), L"ui->tableWidget->verticalHeader()无效", L"paramodel", L"2024-02-20");
        DBG_WARN_AND_RETURN_VOID_UNLESS(ui->tableWidget->horizontalHeader(), L"ui->tableWidget->horizontalHeader()无效", L"paramodel", L"2024-02-20");
        ui->tableWidget->verticalHeader()->setVisible(false);
        ui->tableWidget->horizontalHeader()->setVisible(false);
        ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);

        ui->tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
        ui->tableWidget->setColumnWidth(0,120 );
        ui->tableWidget->setColumnWidth(1,300 );
        ui->tableWidget->setColumnWidth(2,240 );

        // 标题
        std::vector<std::wstring> strVec;
        strVec.emplace_back(L"函数");
        strVec.emplace_back(L"输入值-返回值");
        strVec.emplace_back(L"使用介绍");

        strVec.emplace_back(L"sin()");
        strVec.emplace_back(L"输入值：度，返回值：数值。");
        strVec.emplace_back(L"例如：sin(β)=a/c");

        strVec.emplace_back(L"cos()");
        strVec.emplace_back(L"输入值：度，返回值：数值。");
        strVec.emplace_back(L"例如：cos(β)=b/c");

        strVec.emplace_back(L"tan()");
        strVec.emplace_back(L"输入值：度，返回值：数值。");
        strVec.emplace_back(L"例如：tan(β)=a/b");

        strVec.emplace_back(L"asin()");
        strVec.emplace_back(L"输入值：数值，返回值：度。");
        strVec.emplace_back(L"例如：asin(a/c)=β");

        strVec.emplace_back(L"acos()");
        strVec.emplace_back(L"输入值：数值，返回值：度。");
        strVec.emplace_back(L"例如：acos(b/c)=β");

        strVec.emplace_back(L"atan()");
        strVec.emplace_back(L"输入值：数值，返回值：度。");
        strVec.emplace_back(L"例如：atan(a/b)=β");

        strVec.emplace_back(L"sqrt()");
        strVec.emplace_back(L"开平方函数，输入值：所有类型，返回值：与输入值相同");
        strVec.emplace_back(L"例如：A=9，B=sqrt(A)=3");

        strVec.emplace_back(L"pow()");
        strVec.emplace_back(L"幂函数，输入值：所有类型，返回值：单位的平方");
        strVec.emplace_back(L"例如：3^2=pow(3,2)=9");

        strVec.emplace_back(L"if(判断条件，条件正确返回的值,条件不正确返回的值)");
        strVec.emplace_back(L"条件语句,返回值类型为条件判断设置时的返回值类型");
        strVec.emplace_back(L"例如：b=if(a>50,200,100)；\na = 100时，b = 200；\na = 10时，b = 100。");

        strVec.emplace_back(L"Round()");
        strVec.emplace_back(L"四舍五入取整，输入值和返回值单位类型一致");
        strVec.emplace_back(L"例如：a=100.5m，Round(a)=101m");

        strVec.emplace_back(L"Rounddown()");
        strVec.emplace_back(L"向下取整，输入值和返回值单位一致");
        strVec.emplace_back(L"例如：a=100.5，Rounddown(a)=100");

        strVec.emplace_back(L"Roundup()");
        strVec.emplace_back(L"向上取整，输入值和返回值单位一致");
        strVec.emplace_back(L"例如：a=100.5度，Roundup(a)=101度");

        strVec.emplace_back(L"size_lookup(查找表格名, 赋值列标题, 找不到时的默认值, 查找依据1, ..., 查找依据N)");
        strVec.emplace_back(L"返回值类型与赋值列的参数类型一致");
        strVec.emplace_back(L"以查找表格的通用模板为例：a=2, b=2, size_lookup('查找表格','列标题3',100,a,b)=1.5");

        int index = 0;
        for (int row = 0; row < 14; ++row)
        {
            for (int column = 0; column < 3; ++column)
            {
                OwnerPtr<QTableWidgetItem> item = NEW_AS_OWNER_PTR(QTableWidgetItem);
                item->setText(QString::fromStdWString(strVec[index]));

                if (ui->tableWidget)
                {
                    ui->tableWidget->setItem(row, column, TRANSFER_OWNERSHIP_TO_THIRD_PARTY(item));
                    if ( ui->tableWidget->item(row, column))
                    {
                        ui->tableWidget->item(row, column)->setTextAlignment(Qt::AlignCenter);
                    }
                }

                ++index;
            }
        }
        ui->tableWidget->setMouseTracking(true);
        connect(ui->closeBtn, &QPushButton::clicked, this, &ExprAssistantDlg::CloseDlg);
        connect(ui->gbmp_title, &GmTitleBar::closeRequested, this, &ExprAssistantDlg::CloseDlg);
        connect(ui->gbmp_title, &GmTitleBar::moveRequested, this, &ExprAssistantDlg::onMoveRequested);
        connect(ui->tableWidget, SIGNAL(entered(QModelIndex)), this, SLOT(ShowTooltip(QModelIndex)));
        setFixedSize(size());
    }

    void ExprAssistantDlg::ShowTooltip(QModelIndex index)
    {
        QToolTip::showText(QCursor::pos(), index.data().toString());
    }

    void ExprAssistantDlg::onMoveRequested(const QPoint& cursorPos, const QPoint& offset)
    {
        Q_UNUSED(cursorPos);
        move(pos() + offset);
    }

    ExprAssistantDlg::~ExprAssistantDlg()
    {

    }

    void ExprAssistantDlg::CloseDlg()
    {
        QDialog::reject();
    }

}

